<?php
class brand extends CI_Controller
{
    public function __construct()
    {
        parent::__construct();
        $this->load->model("brand_model");
        $this->load->library('pagination');
        $this->load->helper('url');
    }
    public function index()
    {
        $data['title'] = "List Brand";
        $data['path'] = 'brand/list' ;

        //css pagination
        $config['full_tag_open'] = '<ul class="tsc_pagination tsc_paginationA tsc_paginationA01">';
        $config['full_tag_close'] = '</ul>';
        $config['prev_link'] = '&lt;';
        $config['prev_tag_open'] = '<li>';
        $config['prev_tag_close'] = '</li>';
        $config['next_link'] = '&gt;';
        $config['next_tag_open'] = '<li>';
        $config['next_tag_close'] = '</li>';
        $config['cur_tag_open'] = '<li class="current"><a href="#">';
        $config['cur_tag_close'] = '</a></li>';
        $config['num_tag_open'] = '<li>';
        $config['num_tag_close'] = '</li>';

        $config['first_tag_open'] = '<li>';
        $config['first_tag_close'] = '</li>';
        $config['last_tag_open'] = '<li>';
        $config['last_tag_close'] = '</li>';

        $config['first_link'] = '&lt;&lt;';
        $config['last_link'] = '&gt;&gt;';
        //end

        $config['uri_segment'] = 4;
        $config['per_page'] = 4;
        $config['total_rows'] = $this->brand_model->totalBrand();
        $config['base_url'] = base_url().'admincp/brand/index';
        $this->pagination->initialize($config);
        $start = $this->uri->segment(4);
        $data['listBrand'] = $this->brand_model->listBrand($config['per_page'],$start);
        $this->load->view("dashboard/index",$data);
    }
    public function SortName() {
        $data['title'] = "List Brand";
        $data['path'] = 'brand/list' ;
        $results = $this->brand_model->get_item_list();
        $data['listBrand'] = $results['item_list'];
        $this->load->view('dashboard/index', $data);
    }

    public function insertBrand()
    {
        $data['title'] = "Add Brand";
        $data['path'] = "brand/add";
        $data['errors'] = "";
        if ($this->input->post('submit') != NULL) {
            $this->form_validation->set_rules('name', 'BrandName', 'required');
            if ($this->form_validation->run() == NULL) {
                $this->load->view("dashboard/index", $data);
            } else {
                $config['upload_path'] = './images/brand';
                $config['allowed_types'] = 'gif|jpg|png';
                $config['encrypt_name'] = TRUE;
                $this->load->library('upload', $config);
                $this->upload->initialize($config);
                if (!$this->upload->do_upload('images')) {

                    $data['errors'] = array('error' => $this->upload->display_errors());

                    $this->load->view('drashboard/index', $data);
                } else {
                    $name_img = $this->upload->data();
                    $name_image = $name_img['file_name'];
                    $name = $this->input->post('name');
                    $info = $this->input->post('info');
                    $dataarray = array(
                        'brand_name' => $name,
                        'brand_logo' => $name_image,
                        'brand_info' => $info
                    );
                    $checkname = $this->brand_model->checkName($name);
                    if ($checkname == FALSE) {
                        $data['errors'] = "Thương hiệu đã tồn tại";
                        $this->load->view("dashboard/index", $data);
                    } else {
                        $this->brand_model->addBrand($dataarray);
                        redirect(base_url() . "admincp/brand");
                        exit();
                    }
                }
            }
        } else {
            $this->load->view("dashboard/index", $data);
        }
    }

    public function deleteBrand(){
        $id = $this->uri->segment(4);
        if(isset($_POST['check'])){
            $a = $this->input->post('check');
            if($a == $id){
                
            }
        }
    }
    public function deleteProduct(){
        $id = $this->uri->segment(4);
        $this->brand_model->delBrand($id);
        $this->brand_model->delProduct($id);
        redirect(base_url()."admincp/brand");
        exit();
    }
}